package com.ceuer.resttemplate.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ceuer.resttemplate.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//其实POST请求方法和GET请求方法上大同小异，RestTemplate的POST请求也包含两个主要方法：
// postForObject()
// postForEntity()
//postForEntity()返回全部的信息，postForObject()方法返回body对象，具体使用方法如下！
@RestController
public class PostController {
	/**
	 * 模拟表单请求，post方法测试
	 * @return JSONObject
	 */
	@RequestMapping(value = "testPostByForm", method = RequestMethod.POST)
	public JSONObject testPostByForm(@RequestParam("userName") String userName, @RequestParam("userPwd") String userPwd) {
		JSONObject result = new JSONObject();
		result.put("code", 200);
		result.put("msg", "请求成功，方法：testPostByForm，请求参数userName：" + userName + ",userPwd:" + userPwd);
		return result;
	}
	
	/**
	 * 模拟表单请求，post方法测试
	 * @param user
	 * @return JSONObject
	 */
	@RequestMapping(value = "testPostByFormAndObj", method = RequestMethod.POST)
	public JSONObject testPostByForm(User user) {
		JSONObject result = new JSONObject();
		result.put("code", 200);
		result.put("msg", "请求成功，方法：testPostByFormAndObj，请求参数：" + JSON.toJSONString(user));
		return result;
	}
	
}
